/**
 * @Title: RuleController.java
 * @Package com.lemon.controller.cityRun
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年10月16日
 * @version V1.0
 */
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.controller.BaseController;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrShops;
import com.lemon.model.cityRun.CrSysRuleGroup;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.service.cityRun.CrShopsService;
import com.lemon.service.cityRun.CrSysRuleService;
import com.lemon.utils.MD5;
import com.lemon.utils.ReqResult;
import com.lemon.utils.TokenUtils;

/**
 * <p>
 * Title: RuleController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年10月16日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/cru")
public class LoginController extends BaseController {

	@Resource
	private CrShopUserService userService;
	@Resource
	private CrShopsService shopService;
	@Resource
	private CrSysRuleService ruleService;

	//获取全部用户信息
	/**
	 * <b>Description:<blockquote> 登陆接口
	 * </blockquote></b>
	 * 
	 * @Title: login
	 * @param au      用户名
	 * @param pd      密码
	 * @param request
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月17日
	 */
	@RequestMapping(value = "/login.do" , method = RequestMethod.POST)
	public ReqResult login(String au , String pd , HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopUser user = new CrShopUser();
			user.setUserAccount(au);
			//			CrShopUser u = userService.selectBySelective(user);
			user.setOrderBy(" order by create_time desc ");
			CrShopUser u = userService.selectShopUserByAcc(au);

			if (null != u) {
				//判断密码是否相等
				String pwd = u.getUserPwd();
				if (pd.toUpperCase().equals(pwd.toUpperCase())) {
					//判断账号状态
					if (1 == u.getUserStatus()) {
						//判断账号权限
						CrSysRuleGroup rule = ruleService.selectGroupByPrimaryKey(u.getRuleId());
						if (rule == null || rule.getStatus() != 1) {
							reqResult.setReturnResult("账号无权限" , ReqResult.code_no_permission);
							return reqResult;
						}
						/*
						 * CrShops css = new CrShops();
						 * css.setShopUserId(u.getId());
						 * CrShops uu = shopService.selectShopAndUserBySel(css);
						 * u.setShopTitle(uu.getShopTitle());
						 */
						request.getSession().setAttribute("user" , u);
						reqResult.setReturnResult("succ" , ReqResult.code_Success);
						//生成token令牌
						String token = TokenUtils.createJwtToken(u.getUserAccount());
						response.setHeader("accessToken" , token);
						CrShops cs = new CrShops();
						cs.setShopUserId(u.getId());
						List<CrShops> list = shopService.selectListBySelective(cs);
						Cookie cookie = new Cookie("shop_id", list.size() > 0 ? list.get(0).getId() : "0");
						cookie.setPath("/");
						response.addCookie(cookie);
						//判断用户角色
						if (1 == u.getUserType()) {  //1=管理员  2=门店用户
							Cookie cookie2 = new Cookie("adm", "1");
							cookie2.setPath("/");
							response.addCookie(cookie2);
						} else {
							Cookie cookie2 = new Cookie("adm", "0");
							cookie2.setPath("/");
							response.addCookie(cookie2);
						}
						Cookie cookie3 = new Cookie("uid", u.getId());
						cookie3.setPath("/");
						response.addCookie(cookie3);
						Cookie cookie4 = new Cookie("acc", u.getUserAccount());
						cookie4.setPath("/");
						response.addCookie(cookie4);
						addLogs("登陆" , request , response);
					} else {
						reqResult.setReturnResult("您账号已被封停，请联系客服解封" , ReqResult.code_no_permission);
					}
				} else {
					reqResult.setReturnResult("账号密码错误" , ReqResult.code_login_error);
				}
			} else {
				reqResult.setReturnResult("账号不存在" , ReqResult.code_not_exist);
			}
		} catch (Exception e) {
			// 记录系统错误的登陆记录
			reqResult = new ReqResult("err", ReqResult.code_fail);
			MyException.writeErr(e , "操作失败,请稍后重试");
		}
		return reqResult;
	}

	public static void main(String[] args) {
		String md5 = MD5.getMD5("123456");
		System.out.println(md5);
	}
}
